package com.summer.study.leetcode.thread.sequence_114;

import java.util.concurrent.Semaphore;

/**
 * @auther: 杨杰
 * @date: 2019/7/25 10:44
 * @description:
 */
public class SemaphoreDemo {

    private Semaphore s1 = new Semaphore(1);

    private Semaphore s2 = new Semaphore(1);

    private Semaphore s3 = new Semaphore(1);


    public void first(Runnable printFirst) throws InterruptedException {
        s1.acquire();
        printFirst.run();
        s2.release();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        s2.acquire();
        printSecond.run();
        s3.release();
    }

    public void third(Runnable printThird) throws InterruptedException {
        s3.acquire();
        printThird.run();
    }


    public static void main(String[] args) throws InterruptedException{
        SemaphoreDemo demo=new SemaphoreDemo();
        Foo foo=new Foo();

        demo.first(new Thread(() -> {
            foo.one();
        }));
        demo.second(new Thread(() -> {
            foo.two();
        }));
        demo.third(new Thread(() -> {
            foo.three();
        }));
    }
}
